package com.cash.app.ui.loan

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.activity.viewModels
import com.cash.app.R
import com.cash.app.base.BaseActivity
import com.cash.app.constant.AppConstant.RATING_DISLIKE
import com.cash.app.constant.AppConstant.RATING_LIKE
import com.cash.app.custom.CommonDialog
import com.cash.app.databinding.AcLoanApplyResultBinding
import com.cash.app.databinding.ViewRatingBinding
import com.cash.app.utils.ThirdAppUtils

class LoanConfirmResultActivity : BaseActivity() {

    private var remark = ""
    private var ratingDialog: CommonDialog? = null

    companion object {
        const val EXTRA_REMARK = "E_REMARK"
        fun start(
            context: Context, remark: String
        ) {
            val intent = Intent(context, LoanConfirmResultActivity::class.java).apply {
                putExtra(EXTRA_REMARK, remark)
            }
            context.startActivity(intent)
        }
    }

    private lateinit var binding: AcLoanApplyResultBinding
    private val viewModel: LoanConfirmResultViewModel by viewModels()

    override fun setViewBind() {
        binding = AcLoanApplyResultBinding.inflate(layoutInflater)
    }

    override fun getLayoutView(): View {
        return binding.root
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        remark = intent.getStringExtra(EXTRA_REMARK) ?: ""
        binding.loanApplyResultTip.text = remark

        initObservers()
        viewModel.getCanScore()
    }

    private fun initObservers() {
        viewModel.canScoreData.observe(this) { canScore ->
            if (canScore) {
                showRatingDialog()
            }
        }
    }

    private fun showRatingDialog() {
        val ratingView = ViewRatingBinding.inflate(LayoutInflater.from(this))
        val currentRating = ratingView.ratingBar2.getRating()

//        var currentRating = ratingView.ratingBar.rating.toInt()
//        ratingView.ratingBar.setOnRatingBarChangeListener { _, rating, _ ->
//            currentRating = rating.toInt()
//        }

        ratingDialog = CommonDialog.Builder(this).apply {
            showTitle(true)
            setTitle(getString(R.string.loan_result_rating_title))
            setContentView(ratingView.root)
            setCancelable(true)
            setCancelableOnTouchOutside(true)
            addButton(
                text = getString(R.string.dislike), onClick = { dialog ->
                    viewModel.submitScore(currentRating, RATING_DISLIKE)
                    dialog.dismiss()
                })
            addButton(
                text = getString(R.string.like), onClick = { dialog ->
                    viewModel.submitScore(currentRating, RATING_LIKE)
                    ThirdAppUtils.gotoGPInBrowser(this@LoanConfirmResultActivity)
                    dialog.dismiss()
                    finish()
                }, textColor = R.color.red_1
            )
        }.build()

        ratingDialog?.show()
    }

    override fun onDestroy() {
        super.onDestroy()
        ratingDialog?.dismiss()
    }
} 